package com.simple.mqtt.server.auth;

import java.util.Objects;

/**
 * 简单认证处理器
 * 提供基本的用户名/密码认证，支持匿名访问
 */
public class SimpleMqttAuthHandler implements IMqttAuthHandler {
    private final String username;
    private final String password;
    private final boolean allowAnonymous;

    /**
     * 匿名访问构造函数
     */
    public SimpleMqttAuthHandler() {
        this(null, null, true);
    }

    /**
     * 用户名/密码认证构造函数
     * @param username 期望的用户名
     * @param password 期望的密码
     */
    public SimpleMqttAuthHandler(String username, String password) {
        this(username, password, false);
    }

    /**
     * 私有构造函数
     * @param username 期望的用户名
     * @param password 期望的密码
     * @param allowAnonymous 是否允许匿名访问
     */
    private SimpleMqttAuthHandler(String username, String password, boolean allowAnonymous) {
        this.username = username;
        this.password = password;
        this.allowAnonymous = allowAnonymous;
    }

    @Override
    public boolean authenticate(String username, String password) {
        if (allowAnonymous) {
            return true;
        }
        
        if (this.username != null && this.password != null) {
            return Objects.equals(this.username, username) && Objects.equals(this.password, password);
        }
        
        return true;
    }
}

